(function () {
    'use strict';

    angular.module('app').controller('sys.user', SysUserCtrl);

    SysUserCtrl.$inject = [
        '$rootScope',
        '$scope',
        '$filter',
        '$win',
        '$dialog',
        '$toast',
        'Api',
        'SysUserService',
        'SysUserPassService'
    ];
    function SysUserCtrl($rootScope, $scope, $filter, $win, $dialog, $toast, Api, SysUserService, SysUserPassService) {

        var sysUserDialog;
        var showDialog = function () {
            sysUserDialog = $win.open({
                template: 'sysUserDialog',
                scope   : $scope
            });
        };
        var update = function (ik) {
            $scope.isUpdate = true;
            SysUserService.get(ik)
                .then(function (data) {
                    $scope.sysUser = data;
                    showDialog();
                });
        };
        var remove = function (ik) {
            SysUserService.remove(ik)
                .then(function () {
                    $toast.success('删除成功!');
                    $scope.sysUserTable.reload();
                });
        };

        // ----------------------------------------
        // table
        $scope.options = {
            api     : Api.sys.user.page,
            checkbox: true,
            column  : [{
                text : '序号',
                index: true
            }, {
                text : '昵称',
                name : 'name',
                ratio: 2 //列宽比列, 不配置时默认
            }, {
                text : '角色',
                name : 'roleName',
                ratio: 2 //列宽比列, 不配置时默认
            }, {
                text    : '更新时间',
                name    : 'updatime',
                renderer: function (row) {
                    return row.updatime ? $filter('date')(row.updatime * 1000) : '';
                }
            }, {
                text : '操作列',
                type : 'action',
                items: [{
                    icon   : 'fa fa-pencil-square-o',
                    tooltip: '修改',
                    spin   : false, // 鼠标滑过图标时是否旋转
                    handler: function (o, row) {
                        update(row.ik);
                    }
                }, {
                    icon   : 'fa fa-key',
                    tooltip: '重置密码',
                    spin   : false, // 鼠标滑过图标时是否旋转
                    handler: function (o, row) {
                        SysUserPassService.reset(row.ik)
                            .then(function (result) {
                                if (result.success) {
                                    $toast.success('操作成功!');
                                } else {
                                    $toast.error(result.msg || '操作失败!');
                                }
                            });
                    }
                }, {
                    icon   : 'fa fa-remove',
                    tooltip: '删除',
                    handler: function (o, row) {
                        if (row.ik === $rootScope.user.ik) {
                            return $toast.warn('请勿删除自己!');
                        }
                        $dialog.confirm('确认删除', function (confirm) {
                            confirm && remove(row.ik);
                        });
                    }
                }]
            }]
        };

        // ----------------------------------------
        // toolbar

        // 初始化 table查询参数(根据 ag-search-params 参数配置的)
        $scope.params = {};

        $scope.search = function (name) {
            $scope.params.userName = name;
        };

        $scope.create = function () {
            $scope.isUpdate = false;
            $scope.sysUser = {};
            showDialog();
        };

        // 跳转添加用户页
        $scope.submit = function () {
            var promise;
            if ($scope.isUpdate) {
                promise = SysUserService.update($scope.sysUser.name, $scope.sysUser.ik);
            } else {
                promise = SysUserService.create($scope.sysUser);
            }
            promise.then(function (result) {
                if (result.success) {
                    $toast.success('操作成功!');
                    sysUserDialog.close();
                    $scope.sysUserTable.reload();
                } else {
                    $toast.error(result.msg || '操作失败!');
                }
            });
        };
    }
})
();